(function($){
	var ques = new Object();
	/*---------------------------------------------------------------------*/
	ques.renderTree = function (jq){
		var node = jq.children("div.node");
		/* render handle */
		node.children("img").toggle(function(){
			var handle = $(this);
			var div = handle.parent();
			var sub = div.children("div"); 
			sub.load(ROOT + "/logined/ques/list.do",{id:div.attr("oid")}
					,function(){
						handle.attr("src",handle.attr("open"));
						ques.renderTree(sub);		
					});
			}
			,function(){
				var handle = $(this);
				handle.parent().children("div").empty();
				handle.attr("src",handle.attr("close"));
		});
		
		/* render text */
		node.children("a").bind("click",function(event){
			ques.open($(this).parent().attr("oid"));
		});
	}
	/*---------------------------------------------------------------------*/
	ques.renderSubjectTree = function (jq){
		var node = jq.children("div.node");
		/* render handle */
		node.children("img").toggle(function(){
			var handle = $(this);
			var div = handle.parent();
			var sub = div.children("div"); 
			sub.load(ROOT + "/logined/subject/list.do",{
							id:div.attr("oid")
							,quesId:ques.id
						}
					,function(){
						handle.attr("src",handle.attr("open"));
						ques.renderSubjectTree(sub);		
					});
			}
			,function(){
				var handle = $(this);
				handle.parent().children("div").empty();
				handle.attr("src",handle.attr("close"));
		});
		
		/* render text */
		node.children("a").bind("click",function(event){
			ques.openSubject($(this).parent().attr("oid"));
		});
	}
	/*---------------------------------------------------------------------*/
	ques.renderEditor = function(){
		var arena = $("#queseditor");
		/* Save data */
		this.id = parseInt(arena.find("input[name='id']").val());
		this.name =  arena.find("input[name='name']").val();
		this.parentId =  parseInt(arena.find("input[name='parentId']").val());
		/* Render buttons */
		var btns = arena.find("td > a");
		// for delete
		$(btns[0]).bind("click",function(){
			var input = $("#queseditor input:hidden");
			arena.load(ROOT + "/logined/ques/del.do",{id:input.val()},function(){
				// Do Nothing...
			});
		})
		// for save
		$(btns[1]).bind("click",function(){
			var inputs = $("#queseditor input,select");
			var d = new Object();
			for(var i=0;i<inputs.length;i++){
				d[inputs[i].name] = inputs[i].value;
			}
			arena.empty();
			arena.load(ROOT + "/logined/ques/save.do",d,function(){
				ques.renderEditor();
			});
		})
		if (this.id > 0) {
			// for new subject
			$(btns[2]).bind("click", function(){
				ques.openSubject(0);
			});
			// for refresh subject
			$(btns[3]).bind("click", function(){
				ques.refreshSubject();
			});
			// load subject
			ques.refreshSubject();
		}else{
			$(btns[2]).addClass("disabled");
			$(btns[3]).addClass("disabled");
			// init subject editor
			$("#subjecteditor").empty();
		}
	}
	/*---------------------------------------------------------------------*/
	ques.open = function(id){
		var arena = $("#queseditor");
		arena.empty();
		arena.load(ROOT + "/logined/ques/edit.do" ,{id:id},function(){
			ques.renderEditor();
		});
	}
	/*---------------------------------------------------------------------*/
	ques.openSubject = function(subjectId,parentId){
		var arena = $("#subjecteditor");
		arena.empty();
		arena.load(ROOT + "/logined/subject/edit.do" ,{
						quesId:ques.id
						,id:subjectId
						,parentId:parentId
					},function(){
			ques.renderSubjectEditor();
		});
	}
	/*---------------------------------------------------------------------*/
	ques.refresh = function (){
		$("#chute").empty();
		$("#subjecttree").empty();
		$("#queseditor").empty();
		$("#chute").load(ROOT + "/logined/ques/list.do",null,function(){
			ques.renderTree($("#chute"));
		});
	}
	/*---------------------------------------------------------------------*/
	ques.refreshSubject = function (){
		$("#subjecttree").empty();
		$("#subjecteditor").empty();
		$("#subjecttree").load(ROOT + "/logined/subject/list.do",{quesId:ques.id},function(){
			ques.renderSubjectTree($("#subjecttree"));
		});
	}
	/*---------------------------------------------------------------------*/
	ques.renderSubjectEditor = function(){
		var arena = $("#subjecteditor");
		/* Save data */
		if(this.subject)
			this.subject = null;
		this.subject = new Object();
		var eles = arena.find("input,textarea,select");
		for(var i=0;i<eles.length;i++){
				this.subject[eles[i].name] = eles[i].value;
		}
		
		/* Render buttons */
		var btns = arena.find(".sbar a");
		// for delete
		$(btns[0]).bind("click",function(){
			var input = $("#subjecteditor input[name='id']");
			arena.load(ROOT + "/logined/subject/del.do",{id:input.val()},function(){
				// Do Nothing...
			});
		});
		// for save
		$(btns[1]).bind("click",function(){
			var eles = $("#subjecteditor input,textarea,select")
							.not(".grideditor").not(":checkbox");
			var d = new Object();
			for(var i=0;i<eles.length;i++){
				d[eles[i].name] = eles[i].value;
			}
			var cbs = $("#subjecteditor :checkbox[name]");
			for(var i=0;i<cbs.length;i++){
				d[cbs[i].name] = cbs[i].checked;
			}
			// get options
			eles = $("#subjecteditor .grideditor");
			d.optionValues = new Array();
			for(var i=0;i<eles.length;i++){
				d.optionValues.push(eles[i].value);
			}
			eles = $("#subjecteditor :checkbox").not("[name]");
			d.optionChecks = new Array();
			for(var i=0;i<eles.length;i++){
				if(eles[i].checked)
					d.optionChecks.push(1);
				else
					d.optionChecks.push(0);
			}
			arena.empty();
			arena.load(ROOT + "/logined/subject/save.do",d,function(){
				ques.renderSubjectEditor();
			});
		});
		// for add new subject
		if(btns.length>2){
			$(btns[2]).bind("click",function(){
				ques.openSubject(0,$("#subjecteditor input[name='id']").val())
			});
		}
		/* ~ end render buttons */
		
		// for add options button in editor
		arena.find("td.optionbar a").bind("click",function(){
			var row = $("<tr><td><input class=grideditor name=optText></td><td><input type=checkbox></td><td><img class=icon16 src=" + ROOT + "/images/del.gif" + "></td></tr>");
			arena.find(".options table").append(row);
			row.find("img").one("click",function(){
				$($(this).parent()[0]).parent().remove();
			});
		});
		arena.find(".options img").one("click",function(){
			$($(this).parent()[0]).parent().remove();
		});
		
	}
	/*---------------------------------------------------------------------*/
	
	// Install to jQuery
	$.ques = ques;
})(jQuery);
/*
 * ============================================
 * = main method 
 * ============================================
 */
(function($){
	/* main method */
	$(document).ready(function(){
		$.ques.refresh();
		
		/* render toolbar*/
		var btns = $("#toolbar a");
		// refresj button;
		$(btns[0]).bind("click",function(){
			$.ques.refresh();
		});
		// new questionnaire button;
		$(btns[1]).bind("click",function(){
			$.ques.open(0);
		});
		/* ~ end redner toolbar */
		
		var menus = $("#menubar a");
		$(menus[0]).bind("click",function(){
			window.open(ROOT + "/logined/consteditor.jsp");
		});
	});
})(jQuery);